﻿using UnityEngine;
using System.Collections;

public class TreeCCL : MonoBehaviour {

 
    public Transform[] go;
   //  //存储游戏当前需要生成的树木
    private  GameObject num1;
    private  GameObject num2;

    private GameObject tree10;
    private GameObject tree11;
    private GameObject tree20;
    private GameObject tree21;
    private GameObject tree30;
    private GameObject tree31;


    public GameObject bg;





    private GameObject tg;
    private float me;


    private Vector3[] gopoint = new Vector3[12];

    private Vector3 to;

    private GameObject tempGO;
    private Vector3 scall;


    private bool CanInstance = true;
    private manCCL man;


    void Awake() 
    {
        Application.targetFrameRate = 30;
        tree10 = Resources.Load("tree_0_0") as GameObject;
        tree11 = Resources.Load("tree_0_1") as GameObject;
        tree20 = Resources.Load("tree_1_0") as GameObject;
        tree21 = Resources.Load("tree_1_1") as GameObject;
        tree30 = Resources.Load("tree_2_0") as GameObject;
        tree31 = Resources.Load("tree_2_1") as GameObject;
    }
  //  private Vector3 peopleLeft;
  //  private Vector3 peopleRight;

  //  private int touch;

    int num = 0;
    void Start() 
    {
        num1 = tree10;
        num2 = tree11;
        int i=0;
        foreach (Transform item in go)
        {

            gopoint[i] = item.position;
            i += 1;
        }
     /*   peopleLeft = new Vector3(-1.964f, -3.84f, 0);
        peopleRight = new Vector3(1.964f, -3.84f, 0);
        
        touch = Screen.width / 2;
        print(touch);*/
        scall = new Vector3(-1, 1, 1);
        man = GameObject.FindGameObjectWithTag("Player").GetComponent<manCCL>();
    }
	// Update is called once per frame
	void Update () 
    {
       



        if (Input.GetMouseButtonDown(0) && man.state==GameState.runing)
        {
            {//根据分数修改树木的颜色和背景
                if (man.numberCount == 40)
                {
                    num1 = tree20;
                    num2 = tree21;
                }
                else if (man.numberCount == 51)//修改根部
                {
                    Sprite sp = Resources.Load("root_1", typeof(Sprite)) as Sprite;
                    go[0].gameObject.GetComponent<SpriteRenderer>().sprite = sp;
                    bg.GetComponent<SpriteRenderer>().sprite = Resources.Load("bg_1", typeof(Sprite)) as Sprite;
                }
                else if (man.numberCount == 80)
                {
                    num1 = tree30;
                    num2 = tree31;
                }
                else if (man.numberCount == 91)
                {
                    Sprite sp = Resources.Load("root_2", typeof(Sprite)) as Sprite;
                    go[0].gameObject.GetComponent<SpriteRenderer>().sprite = sp;
                    bg.GetComponent<SpriteRenderer>().sprite = Resources.Load("bg_2", typeof(Sprite)) as Sprite;
                }
            }

            for (int i = 1; i < go.Length; i++)
            {
                to.x = go[i].position.x;
                to.y = gopoint[i - 1].y;

                go[i].position = to;
            }
            //Destroy(go[1].gameObject);
            //go[1]
            go[1].GetComponent<cclSelf>().moveAndDescory();

            for (int i = 2; i < go.Length; i++)
            {
                go[i - 1] = go[i];
            }
            int index = Random.Range(0, 2);
            GameObject goTop;
            if (index == 0)
            {
                goTop = GameObject.Instantiate(num1, Vector3.up * 7.951f, Quaternion.identity) as GameObject;
                go[11] = goTop.transform;
                CanInstance = true;
            }
            else if (index == 1)
            {
                if (CanInstance)
                {
                    int totel = Random.Range(0, 2);
                    if (totel == 0)
                    {//左边
                        goTop = GameObject.Instantiate(num2, Vector3.up * 7.951f + Vector3.left * 0.81f, Quaternion.identity) as GameObject;
                        goTop.transform.localScale = scall;
                        go[11] = goTop.transform;
                    }
                    else if (totel == 1)
                    {//右边
                        goTop = GameObject.Instantiate(num2, Vector3.up * 7.951f - Vector3.left * 0.81f, Quaternion.identity) as GameObject;
                        go[11] = goTop.transform;
                    }
                    CanInstance = false;
                }
                else
                {
                    goTop = GameObject.Instantiate(num1, Vector3.up * 7.951f, Quaternion.identity) as GameObject;
                    go[11] = goTop.transform;
                    CanInstance = true;
                }
            }
        }
	}
   
}
